package com.zbcz.crm

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.AdapterView
import androidx.activity.viewModels
import com.zbcz.crm.adapter.CustomSpinnerDicItemAdapter
import com.zbcz.crm.adapter.ProductBeanListAdapter
import com.zbcz.crm.bean.DictItem
import com.zbcz.crm.databinding.ActivityProductQueryBinding
import com.zbcz.crm.util.onSingleClick
import com.zbcz.crm.viewmodel.ProductQueryViewModel

class ProductQueryActivity : BaseActivity() {

    private lateinit var mBinding: ActivityProductQueryBinding

    private val mViewModel: ProductQueryViewModel by viewModels()


    private var mDicItemAdapter: CustomSpinnerDicItemAdapter? = null

    private var mCurrentProductType: String = ""
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityProductQueryBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        init()
    }

    fun init() {
        mBinding.ivBack.setOnClickListener {
            finish()
        }
        mViewModel.getDictionItem("product_type")
        mViewModel.mDictItemResult.observe(this) {
            val mData = it?.toMutableList()?.apply {
                add(0, DictItem("", text = "请选择", "", title = "请选择"))
            }
            if (mData != null) {
                mDicItemAdapter = CustomSpinnerDicItemAdapter(this@ProductQueryActivity, mData)
                mBinding.spProjectType.onItemSelectedListener =
                    object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(
                            parent: AdapterView<*>?,
                            view: View,
                            position: Int,
                            id: Long
                        ) {
                            // 获取选中的选项
                            val selectedOption = mData[position]
                            mCurrentProductType = selectedOption.value.toString()
                            Log.i(VisitorAddActivity.TAG, "selected${selectedOption.toString()}")

                        }

                        override fun onNothingSelected(parent: AdapterView<*>?) {
                            // 当没有选项被选中时的处理逻辑
                        }
                    }
            }
            mBinding.spProjectType.adapter = mDicItemAdapter
            Log.i(ProjectQueryActivity.TAG, it.toString())
        }
        mBinding.tvSearch.onSingleClick {
            submitQuery()
        }

        mViewModel.mProductListResult.observe(this) { data ->
            Log.i(ProjectQueryActivity.TAG, data.toString())
            if (data != null) {
                val mAdapter = ProductBeanListAdapter(data)
                mBinding.mList.adapter = mAdapter
            }
            mBinding.emptyGroup.visibility = if (data.isNullOrEmpty()) View.VISIBLE else View.GONE
        }
    }

    private fun submitQuery() {
        val productName = mBinding.edProjectInput.text.toString() ?: ""
        val productType = mCurrentProductType
        mViewModel.getProductList(productName, productType)
    }

}